package servlet;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Board;
import DAO.BoardDao;

public class BoardListAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ActionForward forward=new ActionForward();
		ArrayList <Board> boardIdList = new ArrayList <Board>();
		ArrayList <Board> boardList = new ArrayList <Board>();

		BoardDao boardDao = new BoardDao();
		 HttpSession session=request.getSession();
		 request.setAttribute("checkValue", "checkValue");	
			
			int studyId = request.getParameter("studyId") != null
					&& request.getParameter("studyId") != "" ? Integer
					.parseInt(request.getParameter("studyId")) : 0;
			int userIdCheck = session.getAttribute("userid") != null
					&& session.getAttribute("userid") != "" ? Integer
					.parseInt((String)session.getAttribute("userid")) : 0;
			if (userIdCheck < 1) {
				session.invalidate();
				Util.ShowError(response, "로그인해주세요");
			}
			if (studyId < 1) {
				Util.ShowError(response, "잘못 된 접근입니다");
			}
			
	
		
		boardIdList = boardDao.getAllBoardList(studyId);
		for(int i=0; i<boardIdList.size();i++){
			Board board = boardDao.getBoardInfo(boardIdList.get(i).getBoardId());
			boardList.add(board);
			
		}
	
		request.setAttribute("studyId", studyId);
		request.setAttribute("boardList", boardList);
		
		
		forward.setPath("./study_board.jsp");
		
		return forward;
	}

}
